<?php

namespace app\admin\model;

use think\Model;

class TrainList extends Model
{
    protected $autoWriteTimestamp = true;

    protected $pk = 'train_id';

    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        $page = input('get.page');
        if ($search = input('get.train_title')) $where[] = ['a.train_title', 'like', "%" . $search . "%"];
        if (input('get.level_id')) $where[] = ['a.level_id', '=', input('get.level_id')];
        $list = self::alias('a')
            ->join('user_web b', 'a.user_id = b.user_id')
            ->order('a.train_id', 'desc')
            ->where($where)
            ->field('a.*,b.user_name,b.user_mobile')
            ->paginate($limit);
        foreach ($list as $k => $v) {
            $list[$k]['train_type'] = $v['train_type'] == 1 ? '后台制定' : '自行制定';
            $list[$k]['user_mobile'] = $v['user_mobile'] ?: '-';
            $list[$k]['begin_time'] = date('Y-m-d', $v['begin_time']);
            $list[$k]['end_time'] = date('Y-m-d', $v['end_time']);
            $list[$k]['train_times'] = TrainChild::countTrainTimes($v['train_id']);
        }
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit, 'page' => $page]];
    }
}